package com.qhs.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhs.framework.bean.DataTableView;
import com.qhs.framework.bean.PageView;
import com.qhs.sys.entity.SysMenuInfo;
import com.qhs.sys.service.MenuService;
import com.qhs.framework.utils.JsonUtil;
import com.qhs.framework.utils.Result;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by xinchao on 2017/1/12 0012.
 */
@Controller
@RequestMapping(value = {"menu"})
public class MenuInfoController {

    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private MenuService menuService;


    /**
     * 查询获取菜单列表
     * @return
     */
    @RequestMapping(value = {"info"})
    public ModelAndView index() {
        ModelAndView model= new ModelAndView("sys/menu/index");
        return model;
    }

    /**
     * 进入查看页面
     * @param sysMenuInfo
     * @return
     */
    @RequestMapping(value="/{id}", method= RequestMethod.GET)
    public ModelAndView viewPage(SysMenuInfo sysMenuInfo,@PathVariable Long id) {
        sysMenuInfo = menuService.selectByPrimaryKey(id);
        ModelAndView model= new ModelAndView("sys/user/view","sysMenuInfo",sysMenuInfo);
        return model;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value="/del/{id}", method= RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable Long id) {

        if(menuService.deleteByPrimaryKey(id)==0){
            return new Result("删除失败");
        }
        return new Result(true,"删除成功");
    }


    /**
     * 根据角色id 获取菜单树
     * @param id
     * @return
     */
    @RequestMapping(value = "/pro/{id}", method= RequestMethod.POST)
    @ResponseBody
    public Result MenuTree(@PathVariable Long id) {

        return new Result(true,"",menuService.findMenuTree(id));

    }

    /**
     * 校验提交信息
     * @param sysMenuInfo
     * @return
     */
    private String validateForm(SysMenuInfo sysMenuInfo){
        String msg = "";
        //Strings.isNullOrEmpty("");
        if(StringUtils.isEmpty(sysMenuInfo.getMenuName())){
            msg= "菜单名称不能为空";
        }
        if(StringUtils.isEmpty(sysMenuInfo.getMenuCode())){
            msg= "菜单编码不能为空";
        }
        if(sysMenuInfo.getMenuType()==null){
            msg= "菜单类型不能为空";
        }
        return msg;
    }

    /**
     * 查询列表
     * @param sysMenuInfo
     * @param pageView
     * @return
     */
    @RequestMapping(value = {"list"})
    @ResponseBody
    public String searchList(SysMenuInfo sysMenuInfo, PageView pageView) {
        PageHelper.offsetPage(pageView.getStart(),pageView.getLength());

        List<SysMenuInfo> sysMenuInfos = menuService.findMenuByWhere(sysMenuInfo);
        PageInfo<SysMenuInfo> pageInfo = new PageInfo<>(sysMenuInfos);
        DataTableView dataTableView = new DataTableView();

        dataTableView.setDraw(pageView.getDraw());
        dataTableView.setRecordsTotal(new Long(pageInfo.getTotal()).intValue());

        dataTableView.setRecordsFiltered(new Long(pageInfo.getTotal()).intValue());
        dataTableView.setData(sysMenuInfos);
        return JsonUtil.toJson(dataTableView);
    }
    /**
     * 保存角色信息
     * @param sysMenuInfo
     * @return
     */
    @RequestMapping(value = "/add", method= RequestMethod.POST)
    @ResponseBody
    public Result addMenu(SysMenuInfo sysMenuInfo) {

        String valMes= validateForm(sysMenuInfo);
        if(StringUtils.isNotEmpty(valMes)){
            return new Result(valMes);
        }

        if(menuService.insert(sysMenuInfo)==0){
            return new Result("保存失败");
        }

        return new Result(true,"保存成功");

    }


    /**
     * 修改菜单信息
     * @param sysMenuInfo
     * @return
     */
    @RequestMapping(value = "/edit", method= RequestMethod.POST)
    @ResponseBody
    public Result updateRole(SysMenuInfo sysMenuInfo) {

        String valMes= validateForm(sysMenuInfo);
        if(StringUtils.isNotEmpty(valMes)){
            return new Result(valMes);
        }

        if(menuService.updateByPrimaryKeySelective(sysMenuInfo)==0){
            return new Result("修改失败");
        }
        return new Result(true,"修改成功");

    }


    @RequestMapping(value = {"all"})
    @ResponseBody
    public String searchAll() {
        SysMenuInfo sysMenuInfo = new SysMenuInfo();
        sysMenuInfo.setMenuType(0);
        List<SysMenuInfo> menus = menuService.findMenuByWhere(sysMenuInfo);
        return JsonUtil.toJson(menus);
    }

}
